package com.qf.bbapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.bbapi.dao.AddressDao;
import com.qf.bbapi.entity.Address;
import com.qf.bbapi.service.intf.AddressService;
import com.qf.bbapi.util.StringUtil;
import com.qf.bbapi.vo.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {
    @Resource
    private AddressDao addressDao;

    @Override
    public R selectAllAddress(int uid) {
        if(uid>0){
            QueryWrapper<Address> wrapper = new QueryWrapper<>();
            wrapper.eq("uid",uid);
            List<Address> addressList = addressDao.selectList(wrapper);
            if(addressList.size()==0){
                return R.ok(null);
            }else{
                return R.ok(addressList);
            }
        }
        return R.fail();
    }

    @Override
    public R addAddress(Address address) {
        if(address!=null){
            if(StringUtil.isNotEmpty(address.getUid()+"",address.getArea(),address.getDoorplate(),address.getPhone(),address.getName())){
                if(addressDao.insert(new Address(address.getUid(),address.getName(),address.getSex(),address.getPhone(),address.getArea(),address.getDoorplate(),address.getFlag()))>0){
//                    List<Address> uid = addressDao.selectList(new QueryWrapper<Address>().eq("uid", address.getUid()));
//                    for (Address address1 : uid) {
//
//                        System.out.println(address1);
//                    }
                    return R.ok("新增地址成功！");
                }else{
                   return R.fail("添加失败！");
                }
            }
            return R.fail("信息填写不完善！");
        }
        return R.fail();
    }

    @Override
    public R selectOne(int id) {
        if(id>0){
            QueryWrapper<Address> wrapper = new QueryWrapper<>();
            wrapper.eq("id",id);
            Address address = addressDao.selectOne(wrapper);
            if(address!=null){
                return R.ok(address);
            }
        }
        return R.fail();
    }

    @Override
    public R update(Address address) {
        if(address!=null){
            int i = addressDao.updateById(address);
            if(i>0){
                return R.ok();
            }
        }
        return R.fail();
    }

    @Override
    public R delete(int id) {
        if(id>0){
            if(addressDao.deleteById(id)>0){
                return R.ok();
            }else{
                return R.fail();
            }
        }
        return R.fail();
    }
}
